package com.medusa.gruul.payment.service.handler.merchant;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ObjectUtil;
import com.egzosn.pay.common.bean.CertStoreType;
import com.egzosn.pay.common.util.sign.SignUtils;
import com.medusa.gruul.common.model.enums.PayType;
import com.medusa.gruul.common.mp.config.MybatisPlusConfig;
import com.medusa.gruul.common.mp.model.TenantShop;
import com.medusa.gruul.payment.api.entity.MerchantDetails;
import com.medusa.gruul.payment.api.entity.PaymentMerchantConfig;
import com.medusa.gruul.payment.service.common.annotation.MerchantHandler;
import com.medusa.gruul.payment.service.model.dto.MerchantDetailsDTO;
import com.medusa.gruul.payment.service.mp.service.IMerchantDetailsService;
import com.medusa.gruul.payment.service.mp.service.IPaymentMerchantConfigService;
import com.medusa.gruul.payment.service.properties.PaymentProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 支付宝渠道配置
 *
 * @author xiaoq
 * @ Description 支付宝渠道配置
 * @date 2022-07-14 09:47
 */
@Component
@MerchantHandler(PayType.ALIPAY)
public class AlipayMerchantHandler extends AbstractMerchantHandler {


    private final PaymentProperty payProperty;

    public AlipayMerchantHandler(IMerchantDetailsService merchantDetailsService, IPaymentMerchantConfigService paymentMerchantConfigService, PaymentProperty payProperty) {
        super(merchantDetailsService, paymentMerchantConfigService);
        this.payProperty = payProperty;
    }

    /**
     * 商户抽象方法 支付宝渠道配置
     *
     * @param alipayMerchant 支付商户渠道配置DTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handle(MerchantDetailsDTO alipayMerchant) {
        // id不存在进行商户新增
        MerchantDetails details = new MerchantDetails();
        if (ObjectUtil.isEmpty(alipayMerchant.getDetailsId())) {
            //固定值
            details.setPayType(PayType.ALIPAY)
                    .setCertStoreType(CertStoreType.PATH.name())
                    .setSignType(SignUtils.RSA2.getName())
                    .setInputCharset(CharsetUtil.UTF_8)
                    .setIsTest(false)
                    //动态值
                    .setAppid(alipayMerchant.getAppid())
                    .setKeyPrivate(alipayMerchant.getKeyPrivate())
                    .setKeyPublic(alipayMerchant.getKeyPublic())
                    .setKeyCert(alipayMerchant.getKeyCert())
                    .setSubjectName(alipayMerchant.getSubjectName());
            String detailsId = MybatisPlusConfig.IDENTIFIER_GENERATOR.nextUUID(details);
            details.setDetailsId(detailsId);
            details.setNotifyUrl(payProperty.getNotifyUrlPrefix() + "/merchant/pay/notify/" + detailsId);
            details.setReturnUrl(payProperty.getReturnUrl());
            // 保存支付商户渠道信息
            TenantShop.disable(() -> merchantDetailsService.save(details));
            if (CollectionUtil.isNotEmpty(alipayMerchant.getPlatforms())) {
                alipayMerchant.getPlatforms().forEach(platform -> {
                    PaymentMerchantConfig paymentMerchantConfig = new PaymentMerchantConfig();
                    paymentMerchantConfig.setDetailsId(detailsId);
                    paymentMerchantConfig.setPlatform(platform);
                    paymentMerchantConfigService.save(paymentMerchantConfig);
                });
            }
            return;
        }
        // 存在进行商户支付信息修改 
        details.setDetailsId(alipayMerchant.getDetailsId())
                .setAppid(alipayMerchant.getAppid())
                .setKeyPrivate(alipayMerchant.getKeyPrivate())
                .setSubjectName(alipayMerchant.getSubjectName())
                .setKeyPublic(alipayMerchant.getKeyPublic())
                .setKeyCert(alipayMerchant.getKeyCert());
        merchantDetailsService.updateById(details);
        super.paymentMerchantConfigSave(alipayMerchant);

    }

}
